package com.naiterui.ehp.ps.form.controller.inner;

import com.naiterui.ehp.bp.bo.form.DesignFormBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.ps.form.service.IDesignFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;

@Slf4j
@Validated
@Api(tags = {SwaggerTag.INNER_API + SwaggerTag.DESIGN_FORM})
@RestController
@AllArgsConstructor
@RequestMapping("form/inner/design")
public class InnerDesignFormController {

    private final IDesignFormService designFormService;

    /**
     * 获取表单设计
     */
    @GetMapping("/{formId}")
    @ApiOperation(value = "获取表单设计", notes = "获取表单设计", httpMethod = "GET")
    @ApiImplicitParam(name = "formId", value = "formId", required = true, dataType = "String", paramType = "path")
    public ResponseVO<DesignFormBO> getDesignFormVO(@PathVariable String formId) {
        log.info("获取表单设计 请求参数：formId = {}", formId);
        return PublicService.returnResponseVO(this.designFormService.getDesignFormVO(formId));
    }

    /**
     * 保存表单设计
     */
    @PostMapping
    @ApiOperation(value = "保存表单设计", notes = "保存表单设计", httpMethod = "POST")
    public ResponseVO<String> saveDesignForm(@Valid @RequestBody DesignFormBO designForm) {
        log.info("保存表单设计：designForm = {}", designForm);
        return PublicService.returnResponseVO(this.designFormService.saveDesignForm(designForm));
    }

    @GetMapping("/copy/{formId}")
    @ApiOperation(value = "复制表单设计", notes = "复制表单设计", httpMethod = "GET")
    @ApiImplicitParam(name = "formId", value = "formId", required = true, dataType = "String", paramType = "path")
    public ResponseVO<String> copyDesignForm(@PathVariable String formId) {
        log.info("复制表单请求参数：formId = {}", formId);
        return PublicService.returnResponseVO(this.designFormService.copyDesignForm(formId));
    }

    @GetMapping("clone/{sourceId}/{targetId}")
    @ApiOperation(value = "表单结构克隆", notes = "表单结构克隆", httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sourceId", value = "targetId", required = true, dataType = "String",
            paramType = "path"),
        @ApiImplicitParam(name = "targetId", value = "targetId", required = true, dataType = "String",
            paramType = "path")})
    public ResponseVO<String> cloneDesignForm(@PathVariable String sourceId, @PathVariable String targetId) {
        log.info("表单结构克隆请求参数：sourceId = {}，targetId = {}", sourceId, targetId);
        return PublicService.returnResponseVO(this.designFormService.cloneDesignForm(sourceId, targetId));
    }

    /**
     * 获取表单设计和数据
     */
    @GetMapping("/data/{formId}/{businessId}")
    @ApiOperation(value = "获取表单设计和数据", notes = "获取表单设计和数据", httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "dataId", value = "dataId", required = true, dataType = "String", paramType = "path")})
    public ResponseVO<DesignFormBO> getDesignFormDataVO(@PathVariable String formId, @PathVariable String businessId)
        throws BusinessException {
        log.info("获取表单设计和数据 请求参数：formId = {}, businessId = {}", formId, businessId);
        DesignFormBO designFormDataVO = this.designFormService.getDesignFormDataVO(formId, businessId);
        return PublicService.returnResponseVO(designFormDataVO);
    }

    @PostMapping("/{formId}/{businessId}")
    @ApiOperation(value = "保存表单数据", notes = "保存数据", httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "formId", value = "formId", required = true, dataType = "String", paramType = "path"),
        @ApiImplicitParam(name = "businessId", value = "businessId", required = true, dataType = "String",
            paramType = "path")})
    public ResponseVO<DesignFormBO> saveData(@PathVariable String formId, @PathVariable String businessId,
        @RequestBody Map<String, Object> data) throws BusinessException {
        log.info("保存数据 请求参数：formId = {}, businessId = {}, data = {}", formId, businessId, data);
        this.designFormService.saveData(formId, businessId, data);
        return PublicService.returnResponseVO(null);
    }

    /**
     * 文件上传
     */
    @ApiOperation(value = "文件上传")
    @ApiImplicitParam(name = "file", value = "file", paramType = "query", dataType = "__file", required = true)
    @PostMapping("/upload")
    public ResponseVO<String> upload(@NotNull(message = "上传文件不能为null") MultipartFile file) throws BusinessException {
        String url = this.designFormService.uploadFile(file);
        return PublicService.returnResponseVO(url);
    }

    @PostMapping("cache/save/{formId}/{businessId}")
    @ApiOperation(value = "缓存表单数据", notes = "缓存表单数据", httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "formId", value = "formId", required = true, dataType = "String", paramType = "path"),
        @ApiImplicitParam(name = "businessId", value = "businessId", required = true, dataType = "String",
            paramType = "path")})
    public ResponseVO<DesignFormBO> saveCacheFromData(@PathVariable String formId, @PathVariable String businessId,
        @RequestBody Map<String, Object> data) throws BusinessException {
        log.info("缓存数据 请求参数：formId = {}, businessId = {}, data = {}", formId, businessId, data);
        designFormService.saveCacheFormData(businessId, formId, data);
        return PublicService.returnResponseVO(null);
    }

    @PostMapping("cache/get/{formId}/{businessId}")
    @ApiOperation(value = "获取表单缓存数据", notes = "获取表单缓存数据", httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "formId", value = "formId", required = true, dataType = "String", paramType = "path"),
        @ApiImplicitParam(name = "businessId", value = "businessId", required = true, dataType = "String",
            paramType = "path")})
    public ResponseVO<Map<String, Object>> getCacheFromData(@PathVariable String formId,
        @PathVariable String businessId) throws BusinessException {
        log.info("获取缓存数据 请求参数：formId = {}, businessId = {}", formId, businessId);
        Map<String, Object> data = designFormService.getCacheFormData(businessId, formId);
        return PublicService.returnResponseVO(data);
    }

}
